<template>
  <div>
    <el-card class="login" shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <span>账号注册</span>
      </div>
      <el-form
        :model="form"
        ref="loginRef"
        :rules="rules"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="用户名" prop="account">
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">注册</el-button>
          <el-button @click="register">已有账号</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { reg } from '@/api/login'
export default {
  data: () => ({
    form: {
      account: '',
      password: ''
    },
    rules: {
      account: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
      ]
    }
  }),
  methods: {
    login() {
      this.$refs.loginRef.validate((valid) => {
        if (valid) {
          reg({
            account: this.form.account.trim(),
            password: this.form.password
          }).then(() => {
            this.$router.push('./login')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    register() {
      this.$router.push('./login')
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 500px;
  margin: 150px auto;
}
</style>